﻿$.ajaxSetup({ url: "Proxy.ashx", type: "post", dataType: "json" });

var pageIndex = 0;
var pageSize = 20;
var page_count = 0;

function btnSearch_Click() {

    var cityId = $("#ddl_City").val();
    var areaId = $("#ddl_Area").val();
    var streetId = $("#ddl_Street").val();
    var name = $("#txtName").val();

    $.ajax({
        data: { method: "listParkingSpaces",
            cityId: cityId,
            areaId: areaId,
            streetId: streetId,
            name: name,
            page: pageIndex,
            size: pageSize
        }
        , success: function (json) {
            // 先删除原来的行，否则会多次绑定。
            $("#tbl_ParkingSpaces tr").each(function (trindex, tritem) {
                if (trindex != 0)
                    $(tritem).remove();
            });
            // 填充表格
            $("#tbl_ParkingSpaces").append("#full_ParkingSpaces", json.ListParkingSpaces);

            // 分页
            page_count = Math.ceil(json.RowsCount / pageSize);
            PagerInit();
        }
    });
}

function PagerInit() {
    $(document).ready(function () {
        $("#pager").pager({ pagenumber: pageIndex, pagecount: page_count, buttonClickCallback: PageClick });
    });
}
function PageClick(pageclickednumber) {
    $("#pager").pager({ pagenumber: pageclickednumber, pagecount: page_count, buttonClickCallback: PageClick });
    pageIndex = pageclickednumber;
    btnSearch_Click();
}

function DeleteParkingSpace(id) {
    var flag = confirm("确定要删除此数据吗？");
    if (flag) {
        $.ajax({
            data: { method: "DeleteParkingSpace", id: id
            }
        , success: function (json) {
            var msg = "";
            if (json.isSuccess == "true") {
                msg = "数据删除成功！"
                btnSearch_Click();
            }
            else {
                msg = "数据删除失败，请重试..."
            }
            alert(msg);
        }
        });
    } 
}

function init() {
    BindCity();
    $("#btnSearch").click(function () {
        btnSearch_Click();
    });
}

